﻿
/* 
 * Since 2008 - 2012
 */
package com.hh.bss.project.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;
import static com.hh.bss.util.extjs.JsonHelper.outJsonArray;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;


import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.auth.entity.SystemUser;
import com.hh.bss.auth.service.SystemUserService;
import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.entity.CompanyDept;
import com.hh.bss.customer.entity.CustomerContacter;
import com.hh.bss.customer.vo.query.CustomerContacterQuery;
import com.hh.bss.oa.entity.OaEmailSend;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.talent.service.CvService;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;





@Controller
public class ContractJobCvRecomController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private ContractJobCvRecomService contractJobCvRecomService;
	private ContractJobService contractJobService;
	private SystemUserService systemUserService;
	private CvService cvService;
	private ContractJobCvRecom contractJobCvRecom;
	java.lang.Integer id = null;
	private String[] items;

	public void setContractJobServiceImpl(ContractJobService service) {
		this.contractJobService = service;
	}
	
	/** 通过spring自动注入 */
	public void setContractJobCvRecomServiceImpl(ContractJobCvRecomService service) {
		this.contractJobCvRecomService = service;
		}
	public void setSystemUserServiceImpl(SystemUserService service) {
		this.systemUserService = service;
	}
	
	public void setCvServiceImpl(CvService service) {
		this.cvService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}



	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,ContractJobCvRecomQuery contractJobCvRecomQuery) throws IOException
	{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobCvRecomQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = contractJobCvRecomService.findPage(contractJobCvRecomQuery);
		
		List<ContractJobCvRecom> ContractJobCvRecomlist = (List) page.getResult();
		ListRange<ContractJobCvRecom> resultList = new ListRange<ContractJobCvRecom>();
		resultList.setRows(ContractJobCvRecomlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public String jobRecomPage(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		return "forward:/jsp/project/contractJobCvRecom/contractJobCvRecomPage.jsp";	
	}
	public String managerPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		return "forward:/jsp/jobRecomManagerPage.jsp";
		
	}
	public String cvRecomInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		return "forward:/jsp/project/contractJobCvRecom/cvRecomJobInfo.jsp";	
	}
	public String jobRecomInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		return "forward:/jsp/project/contractJobCvRecom/jobRecomCvInfo.jsp";	
	}
	//人选推荐给职位，用在人才简历里
	public String cvRecomPage(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		return "forward:/jsp/project/contractJobCvRecom/cvRecomJobPage.jsp";	
	}
	public void jobrecomcvlist(HttpServletRequest request,HttpServletResponse response,ContractJobCvRecomQuery contractJobCvRecomQuery) throws IOException
	{			
		List<CustomerContacter> customerContacterlist = contractJobCvRecomService.findJobRecomCv(contractJobCvRecomQuery);		
		outJsonArray(response,customerContacterlist);
	}
	public void  recomDoc(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> responseMap = new HashMap<String, Object>();
		//responseMap.put("A1", "A1-value");
		String jobId=request.getParameter("jobId");
		String recomId=request.getParameter("recomId");
		String cvId=request.getParameter("cvId");
		System.out.println(recomId);
		//contractJobCvRecomService.genRecomDoc(request, StringHelper.parseInteger(recomId, 0));
	}	
	public ModelAndView  recomCv(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> responseMap = new HashMap<String, Object>();
		//responseMap.put("A1", "A1-value");
		String jobId=request.getParameter("jobId");
		String recomId=request.getParameter("recomId");
		String cvId=request.getParameter("cvId");
		
		
		//job
		ContractJobCvRecom contractJobCvRecom =(ContractJobCvRecom)contractJobCvRecomService.getById(StringHelper.parseInteger(recomId, 0));
		//ContractJob contractJob
		if (contractJobCvRecom!=null)  responseMap.put("contractJobCvRecom", contractJobCvRecom);
		ContractJob contractJob=(ContractJob)contractJobService.getById(contractJobCvRecom.getJobId());
		if (contractJob!=null)  responseMap.put("contractJob", contractJob);
		
		//cv
		Map cvMap=cvService.getCvInfoById(contractJobCvRecom.getCvId());
		if (cvMap!=null)  responseMap.put("cvMap", cvMap);
		
//		CvCertificate
//		CvEducation
//		CvSkill
//		CvTrainning
//		CvWork
		List cvCertificateList=contractJobCvRecomService.getEntityDao().getHibernateTemplate().find(" from CvCertificate t where t.cvId=?  ", contractJobCvRecom.getCvId());
		if (cvCertificateList!=null)  responseMap.put("CvCertificateList", cvCertificateList);
		List cvEducation=contractJobCvRecomService.getEntityDao().getHibernateTemplate().find(" from CvEducation t where t.cvId=?  ", contractJobCvRecom.getCvId());
		if (cvEducation!=null)  responseMap.put("CvEducation", cvEducation);
		List cvSkill=contractJobCvRecomService.getEntityDao().getHibernateTemplate().find(" from CvSkill t where t.cvId=?  ", contractJobCvRecom.getCvId());
		if (cvSkill!=null)  responseMap.put("CvSkill", cvSkill);
		List cvTrainning=contractJobCvRecomService.getEntityDao().getHibernateTemplate().find(" from CvTrainning t where t.cvId=?  ", contractJobCvRecom.getCvId());
		if (cvTrainning!=null)  responseMap.put("CvTrainning", cvTrainning);
		List cvWork=contractJobCvRecomService.getEntityDao().getHibernateTemplate().find(" from CvWork t where t.cvId=?  ", contractJobCvRecom.getCvId());
		if (cvWork!=null)  responseMap.put("CvWork", cvWork);
		
		
		
		ModelAndView mav = new ModelAndView("/cvRecom",responseMap);
		return mav;
		
	}
	public void setrecom(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			//取要发送的简历
			String content=URLHelper.getDocumentAsString("http://bss.talentbank.com.cn/project/contractJobCvRecom/recomCv.do");
			System.out.println("content:"+content);
			result.put("success", true);
			result.put("msg", "发送成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void get(HttpServletRequest request,HttpServletResponse response,ContractJobCvRecom contractJobCvRecom) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			//ContractJobCvRecom oldContractJobCvRecom = (ContractJobCvRecom)contractJobCvRecomService.getContractJobCvRecomById(contractJobCvRecom.getId());
                         
			//if(oldContractJobCvRecom!=null) result.put("data", oldContractJobCvRecom);
			List oldContractJobCvRecom = contractJobCvRecomService.getContractJobCvRecomById(contractJobCvRecom.getId());
            
			if(oldContractJobCvRecom!=null) result.put("data", oldContractJobCvRecom.get(0));
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void getsendemail(HttpServletRequest request,HttpServletResponse response,ContractJobCvRecom contractJobCvRecom) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			OaEmailSend oaEmailSend= contractJobCvRecomService.getOaEmailSendByRecomId(contractJobCvRecom.getId());
            //查找操作用户的邮件
//			if (this.getOnlineManager(request, response));
			OnlineUser onlineUser=this.getOnlineManager(request, response).getOnlineUser();
			if (onlineUser!=null) {
				SystemUser systemUser=systemUserService.findByUsername(onlineUser.getUsername());
				if (oaEmailSend!=null&&systemUser!=null){
					oaEmailSend.setSender(systemUser.getEmail());
				}
			}
			//systemUserService.findByUsername(username);
			System.out.println(oaEmailSend.getCvName()+"--"+oaEmailSend.getAttachment()+"--"+oaEmailSend.getSubject());
			
			if(oaEmailSend!=null) result.put("data", oaEmailSend);
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void jobcvrecomlist(HttpServletRequest request,HttpServletResponse response,ContractJobCvRecomQuery contractJobCvRecomQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobCvRecomQuery.setOnlineUser(onlineUser);
		}
		Page page = contractJobCvRecomService.findJobRecomCvPage(contractJobCvRecomQuery);
		
		List<ContractJobCvRecom> ContractJobCvRecomlist = (List) page.getResult();
		ListRange<ContractJobCvRecom> resultList = new ListRange<ContractJobCvRecom>();
		resultList.setRows(ContractJobCvRecomlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}

	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,ContractJobCvRecom contractJobCvRecom) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			com.hh.bss.auth.client.OnlineUser onlineUser=null;
			if (onlineManager!=null){
					onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvRecom.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(contractJobCvRecom,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvRecom.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(contractJobCvRecom,"createDt",new Date());}catch(Exception ex){}
				}
			//处理上传文件
			MultipartHttpServletRequest multipartRequest=null;
			MultipartFile multipartFile =null;
			MultipartFile multipartFileEn =null;
		       try{
		    	   multipartRequest = (MultipartHttpServletRequest) request;
		    	   multipartFile = multipartRequest.getFile("uploadFile");
		    	   multipartFileEn = multipartRequest.getFile("uploadFileEn");
		       }catch(Exception ex){ex.printStackTrace();}
		        
		       boolean autoGen=true;
		       boolean autoEnGen=true;
		       //multipart/form-data 提交模式，且提交了文件fileRes,则处理上传文件
		       if (multipartRequest!=null&&multipartFile!=null&&!multipartFile.isEmpty()){
		    	   //contractJobCvRecomService.saveFile(multipartRequest, multipartRequest, contractJobCvRecom);
		    	   contractJobCvRecomService.saveFile(multipartRequest, multipartFile, contractJobCvRecom,1);
		    	   autoGen=false;
		       }
		       if (multipartRequest!=null&&multipartFileEn!=null&&!multipartFileEn.isEmpty()){
		    	   contractJobCvRecomService.saveFile(multipartRequest, multipartFileEn, contractJobCvRecom,2);			       
		    	   autoEnGen=false;
		       }
		       
             
			contractJobCvRecomService.save(contractJobCvRecom);
			if (autoGen||autoEnGen){
				//取消自动生成
				if (autoGen){
					contractJobCvRecomService.genRecomDoc(request,onlineUser, contractJobCvRecom);					
					contractJobCvRecomService.getEntityDao().getHibernateTemplate().update(contractJobCvRecom);
					contractJobService.getEntityDao().flush();
				}
				//if (autoEnGen)contractJobCvRecomService.genRecomDocEn(request,onlineUser, contractJobCvRecom);				
				//contractJobCvRecomService.update(contractJobCvRecom);
			}
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void autogen(HttpServletRequest request,HttpServletResponse response,ContractJobCvRecom contractJobCvRecom) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        ContractJobCvRecom oldcontractJobCvRecom = (ContractJobCvRecom)contractJobCvRecomService.getById(contractJobCvRecom.getId());
                         
			String type=request.getParameter("type"); //type=1，中文,type=2英文，默认是中文
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
                        com.hh.bss.auth.client.OnlineUser onlineUser=null;
			if (onlineManager!=null){
					 onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvRecom.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldcontractJobCvRecom,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvRecom.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldcontractJobCvRecom,"modifiedDt",new Date());}catch(Exception ex){}
				}
			 if ("2".equals(type)){
				 contractJobCvRecomService.genRecomDocEn(request,onlineUser, oldcontractJobCvRecom);
			 }else 
			     contractJobCvRecomService.genRecomDoc(request,onlineUser, oldcontractJobCvRecom);
			   

			contractJobCvRecomService.update(oldcontractJobCvRecom);
			result.put("success", true);
			if ("2".equals(type))
		  	    result.put("file", oldcontractJobCvRecom.getUploadDocEn());
			else 
				result.put("file", oldcontractJobCvRecom.getUploadDoc());	
			result.put("msg", "生成成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void update(HttpServletRequest request,HttpServletResponse response,ContractJobCvRecom contractJobCvRecom) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
            ContractJobCvRecom oldcontractJobCvRecom = (ContractJobCvRecom)contractJobCvRecomService.getById(contractJobCvRecom.getId());
                         
            bind(request,oldcontractJobCvRecom);
            //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvRecom.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldcontractJobCvRecom,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvRecom.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldcontractJobCvRecom,"modifiedDt",new Date());}catch(Exception ex){}
				}
			//处理上传文件
			MultipartHttpServletRequest multipartRequest=null;
			MultipartFile multipartFile =null;
			MultipartFile multipartFileEn =null;
		       try{
		    	   multipartRequest = (MultipartHttpServletRequest) request;
		    	   multipartFile = multipartRequest.getFile("uploadFile");
		    	   multipartFileEn = multipartRequest.getFile("uploadFileEn");
		       }catch(Exception ex){ex.printStackTrace();}
		        
		       //multipart/form-data 提交模式，且提交了文件fileRes,则处理上传文件
		       if (multipartRequest!=null&&multipartFile!=null&&!multipartFile.isEmpty()){
		    	   //contractJobCvRecomService.saveFile(multipartRequest, multipartRequest, oldcontractJobCvRecom);		
		    	   contractJobCvRecomService.saveFile(multipartRequest, multipartFile, oldcontractJobCvRecom,1);
			       
		       }
		       
		       if (multipartRequest!=null&&multipartFileEn!=null&&!multipartFileEn.isEmpty()){
		    	   //contractJobCvRecomService.saveFile(multipartRequest, multipartRequest, oldcontractJobCvRecom);		
		    	   contractJobCvRecomService.saveFile(multipartRequest, multipartFileEn, oldcontractJobCvRecom,2);
			       
		       }

		       
			contractJobCvRecomService.update(oldcontractJobCvRecom);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				contractJobCvRecomService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
}
